exports.taobaoCrawler = function(url,callBack){
	var page = require('webpage').create();
	var retJsonpObj = null;

	var onSibRequestSuccess = function(obj){
		retJsonpObj = obj;
	};

	page.captureContent = [ /.*/ ];
	page.settings.loadImages = false;

	page.onResourceReceived = function(response) {
		if(response.url.indexOf('detail/sib.htm')>-1){
			//这里回调onSibRequestSuccess方法
			eval(response.body);
		}
	};
	page.onError = function(msg,trace){
		var msgStack = ['ERROR: ' + msg];
		if (trace && trace.length) {
			msgStack.push('TRACE:');
			trace.forEach(function(t) {
				msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function + '")' : ''));
			});
		}
		// uncomment to log into the console 
		// console.error(msgStack.join('\n'));
		//phantom.exit();
	};
	page.open(url,function(status){
		var analyzeObj = page.evaluate(function(){

			//页面中dom提取的信息=========================================================
			var pageTitle = document.querySelector("#J_Title > h3").textContent;	//页面中标题
			var default_img = document.querySelector("#J_ImgBooth").src;	//页面中默认图片地址

			var objArr = [];
			var dls = document.querySelectorAll("div.tb-skin > dl.tb-prop");  //查找所有dl
			for(var a=0;a<dls.length;a++){
				var dlElement = dls[a];
				var dt = dlElement.querySelector("dt"); //查找属性类别
				
				var dataValues = dlElement.querySelectorAll("dd > ul > li");  //所有data-value
				var dataValueArr = [];
				for(var b=0;b<dataValues.length;b++){
				var dataValue = dataValues[b].getAttribute("data-value");
				var title = dataValues[b].querySelector("a > span").textContent;
				dataValueArr[b] = {key:dataValue,value:title};
				}
				objArr[a] = {attrTypes:dt.textContent,dataValues:dataValueArr};
			}
			
			var obj = {pageTitle:pageTitle,detailAttr:objArr,defaultImg:default_img}
			return obj;
		});
		var retObj = {};
		retObj.platform = 'taobao';
		retObj.jsonpObj = retJsonpObj;
		retObj.analyzeObj = analyzeObj;
		callBack(retObj);
		phantom.exit();
	});
}


